package com.slipper.qmt.redis;

import com.slipper.common.util.GsonUtil;
import com.slipper.qmt.task.TaskManager;
import com.slipper.qmt.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

public class RedisReceiver implements MessageListener {
    private static final Logger logger = LoggerFactory.getLogger(RedisReceiver.class);

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String topic = new String(pattern);
        String channel = new String(message.getChannel());
        String body = new String(message.getBody());
        logger.info("接收到消息回调，topic: {}, channel: {}, body: {}", topic, channel, body);
        switch (channel) {
            case "on_order_callback":
                onOrderCallback(body);
                break;
            case "on_order_result_callback":
                onOrderResultCallback(body);
                break;
                case "on_order_error_callback":
                onOrderErrorCallback(body);
                break;
            case "on_deal_callback":
                onDealCallback(body);
                break;
            case "on_cancel_callback":
                onCancelCallback(body);
                break;
        }
    }

    // 下单结果回调
    public void onOrderResultCallback(String data) {
        OrderResultVO orderResultVO = GsonUtil.fromJson(data, OrderResultVO.class);
        TaskManager.add(orderResultVO);
        logger.info("onOrderResultCallback: {}", orderResultVO);
    }

    // 下单回调
    public void onOrderCallback(String data) {
        OrderVO orderVO = GsonUtil.fromJson(data, OrderVO.class);
        TaskManager.add(orderVO);
        logger.info("onOrderCallback: {}", orderVO);
    }

    // 下单错误回调
    public void onOrderErrorCallback(String data) {
        OrderErrorVO orderErrorVO = GsonUtil.fromJson(data, OrderErrorVO.class);
        TaskManager.add(orderErrorVO);
        logger.info("onOrderErrorCallback: {}", orderErrorVO);
    }

    // 成交回调
    public void onDealCallback(String data) {
        DealVO dealVO = GsonUtil.fromJson(data, DealVO.class);
        TaskManager.add(dealVO);
        logger.info("onDealCallback: {}", dealVO);
    }

    // 撤单回调
    public void onCancelCallback(String data) {
        DealVO dealVO = GsonUtil.fromJson(data, DealVO.class);
        TaskManager.add(dealVO);
        logger.info("onDealCallback: {}", dealVO);
    }
}
